document.addEventListener('DOMContentLoaded', function () {
    const form = document.getElementById('reg-course-form');
    const token = localStorage.getItem('access_token');
    form.addEventListener('submit', function (event) {
        event.preventDefault();
        const formData = new FormData(form);
        fetch('/api/course_manage/', {
            method: 'POST',
            headers: {
                'X-CSRFToken': document.querySelector('[name=csrfmiddlewaretoken]').value,
                'Authorization': `Bearer ${token}`,
            },
            body: formData
        })
        .then(response => {
            if (response.ok) {
                return response.json();
            } else {
                throw new Error('提交失败');
            }
        })
        .then(data => {
        if (data.code === "200") {
            alert('课程开设成功！');
            form.reset();
        } else {
            alert(`课程开设失败：${data.message || '未知错误'}`);
        }
})
        .catch(error => { 
            console.error('错误:', error);
            alert('提交失败，请稍后再试！');
        });
    });
});